package com.bdqn.dao.Impl;

import com.bdqn.dao.EmployeeDao;
import com.bdqn.entity.Calendar.Record;
import com.bdqn.entity.CheckWork;
import com.bdqn.entity.Employee;
import com.bdqn.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class EmployeeDaoImpl implements EmployeeDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public Employee findByIdAndPassword(String empId, String password) {
        Employee emp = null;
        try {
            String sql = "select * from employee where empId=? and password=?";
            emp = template.queryForObject(sql,new BeanPropertyRowMapper<Employee>(Employee.class),empId,password);
        } catch (Exception e) {

        }
        return emp;
    }


    @Override
    public CheckWork findCheck(String date, Employee emp) {
        CheckWork checkWork = null;
        String sql = "select * from checkwork where empId=? and date=?";

        try {
            checkWork = template.queryForObject(sql,new BeanPropertyRowMapper<CheckWork>(CheckWork.class),emp.getEmpId(),date);
        } catch (DataAccessException e) {

        }
        return checkWork;
    }

    @Override
    public List<Record> findRecord(String empId, String start, String end) {
        String sql = "select * from checkwork where empId=? and date between ? and ?";
        return template.query(sql,new BeanPropertyRowMapper<Record>(Record.class),empId,start,end);
    }

    @Override
    public boolean updatepasswordByNew(String empId, String newpassword) {
        String sql = "update employee set password=? where empId=?";
        return template.update(sql,newpassword,empId)>0;
    }
}
